package com.reggie.background.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.background.common.R;
import com.reggie.background.common.SMSUtils;
import com.reggie.background.common.ValidateCodeUtils;
import com.reggie.background.entity.User;
import com.reggie.background.exception.CustomException;
import com.reggie.background.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendSMS")
    public R<String> sendSMS(@RequestBody Map<String, String> maps, HttpSession session) {
        //校验手机号码是否为空
        String phone = maps.getOrDefault("phone", "");
        if (StringUtils.isEmpty(phone)) {
            throw new CustomException("手机号码不能为空！");
        }
        //生成验证码
        String code = ValidateCodeUtils.generateValidateCode(6).toString();
        //给手机发送验证码
        boolean result = SMSUtils.sendMessage(phone, code);
        if (!result) {
            throw new CustomException("短息系统繁忙，请稍后再试！");
        }
        //将验证码存储在redis中
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(phone,code,5, TimeUnit.MINUTES);
        log.info("本次验证码为：{}",code);
        return R.success("短信发送成功！");
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody Map<String, String> maps, HttpSession session) {
        //接收前端的数据
        String phone = maps.get("phone");
        String code = maps.get("code");
        //获取redis中对应的手机号码的短信验证码
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code2 = (String) valueOperations.get(phone);
        if (!code.equals(code2)) {
            return R.error("验证码错误！");
        }
        //验证码验证成功后查询是否未注册
        User user = new User();
        user.setPhone(phone);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        queryWrapper.eq(User::getPhone, phone);
        User one = userService.getOne(queryWrapper);
        //未注册用户自动注册到用户表
        if (one == null) {
            userService.save(user);
            one = user;
        }
        //删除redis中的验证码
        redisTemplate.delete(phone);
        session.setAttribute("user", one.getId());
        return R.success("登录成功！");
    }
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        session.removeAttribute("user");
        return R.success("成功退出登录！");

    }
}
